package com.water.note.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.water.note.common.PageResult;
import com.water.note.common.Result;
import com.water.note.dto.LoginParam;
import com.water.note.dto.RegisterParam;
import com.water.note.dto.UserDTO;
import com.water.note.dto.UserUpdateParam;
import com.water.note.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserDTO> login(@Validated @RequestBody LoginParam loginParam) {
        UserDTO userDTO = userService.login(loginParam);
        return Result.success(userDTO);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<UserDTO> register(@Validated @RequestBody RegisterParam registerParam) {
        UserDTO userDTO = userService.register(registerParam);
        return Result.success(userDTO);
    }

    /**
     * 用户退出登录
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        StpUtil.logout();
        return Result.success("退出登录成功");
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/info")
    public Result<UserDTO> getCurrentUserInfo() {
        Long userId = StpUtil.getLoginIdAsLong();
        UserDTO userDTO = userService.getUserById(userId);
        return Result.success(userDTO);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/info")
    public Result<UserDTO> updateUserInfo(@Validated @RequestBody UserUpdateParam updateParam) {
        Long userId = StpUtil.getLoginIdAsLong();
        UserDTO userDTO = userService.updateUserInfo(userId, updateParam);
        return Result.success(userDTO);
    }

    /**
     * 修改密码
     */
    @PutMapping("/password")
    public Result<Boolean> updatePassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean result = userService.updatePassword(userId, oldPassword, newPassword);
        return Result.success(result);
    }

    /**
     * 分页查询用户列表（管理员接口）
     */
    @GetMapping("/list")
    public Result<PageResult<UserDTO>> listUsers(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        PageResult<UserDTO> pageResult = userService.listUsers(page, pageSize, keyword);
        return Result.success(pageResult);
    }

    /**
     * 禁用或启用用户（管理员接口）
     */
    @PutMapping("/{userId}/status")
    public Result<Boolean> updateUserStatus(
            @PathVariable Long userId,
            @RequestParam Integer status) {
        boolean result = userService.updateUserStatus(userId, status);
        return Result.success(result);
    }

    /**
     * 删除用户（管理员接口）
     */
    @DeleteMapping("/{userId}")
    public Result<Boolean> deleteUser(@PathVariable Long userId) {
        boolean result = userService.deleteUser(userId);
        return Result.success(result);
    }
}